from collections import Counter

# 转化带有赔数的数据格式{类型:[赔率,赔数]}
# dx = {"0球":10.5,"1球":6.2}
def to_pei(dx):
    # 排序
    ds = sorted(dx.items(), key=lambda x: x[1], reverse=False)
    # print(ds)
    res = {}
    for i in range(len(ds)):
        d = ds[i]
        res[d[0]] = (d[1], i + 1)
    # print(res)
    return res

# 赔数的数据格式[[赔率,类型]}
# dx = [(3.3,"s2")，(3.5,"s3"),(4,5,"s1")....],列表序号表示赔数，
def to_pei_list(dx):
    # dx 的格式：{"s0":12.4,"s1":6.4,"s2":3.2,.....}
    # 排序: 升  ds = [("s2",3.2),("s3",3.4),....]
    ds = sorted(dx.items(), key=lambda x: x[1], reverse=False)
    return ds

# 球方差
def manual_variance(data):
    n = len(data)
    variance = max(data)
    if n > 1:
        avg = sum(data) / n
        variance = round(sum([(x - avg) ** 2 for x in data]) / (n - 1),2)
    return variance

'''
统计最大间隔
dx：[0,1,1,1,0]
0表示不中，1表示中
'''
def tj(dx):
    # 查看一下多少次中奖一次，间隔多少次
    r = []
    a = 0
    dxsum = sum(dx)
    mzl = 0
    maxjg = 0
    avgjg = 0
    variance = 0
    crrjg = 0
    for d in dx:
        if d == 0:
            a += 1
        else:
            r.append(a)
            a = 0
    dxres = Counter(dx)
    if dxsum:
        mzl = round(dxres.get(1)/len(dx),4)
        maxjg = max(r)
        avgjg = round(sum(r)/len(r),2)
        variance = manual_variance(r)
        crrjg = r[0]
        # print("统计：", dxres,"命中率：",mzl)
        # print("最大间隔：", maxjg)
        # print("平均间隔：", avgjg)
        # print("方差：", variance)
        # print("间隔多久中：", crrjg)

    # 命中率,最大间隔,平均间隔,方差
    return mzl,maxjg,avgjg,variance,crrjg


# 胜平负分析数据格式化数据[12,11,5,3,5,7,9,15],是否售卖数据，如果是，就没有比赛结果
def formatHDAData(m,isselling=False):
    oddsHistory = m.get("fixedBonus").get("oddsHistory")
    # 开奖信息
    matchResultList = m.get("fixedBonus").get("matchResultList")
    #   H胜，D平,A负,C异常
    spfr = "c"
    hspfr = "c"
    if matchResultList:
        for r in matchResultList:
            if r.get("code") == 'HHAD':
                spfr = r.get("combination").lower()
            if r.get("code") == 'HAD':
                hspfr = r.get("combination").lower()

    if isselling:
        awayTeamAbbName = m.get("awayTeamAbbName")
        homeTeamAbbName = m.get("homeTeamAbbName")
    else:
        awayTeamAbbName = m.get("awayTeam")
        homeTeamAbbName = m.get("homeTeam")
    # 胜平负
    hadList = oddsHistory.get("hadList")
    had_data = [0,0,0]
    spf_result = ["c",0,0,0]
    # 有可能没有开设胜平负（只有让球，没有胜平负）
    if hadList:
        had = hadList[-1]
        spf = {"h": float(had.get("h")), "d": float(had.get("d")), "a": float(had.get("a"))}
        spfv = [float(had.get("h")), float(had.get("d")), float(had.get("a"))]
        # 转化带有赔数的数据格式{"s0":[10.5,6]}
        res_spf = to_pei(spf)
        peid_spf = to_pei_list(spf)
        # 构造开奖结果
        spf_result = [spfr, res_spf.get(spfr)[1], res_spf.get(spfr)[0]]  # 胜平负开奖结果（结果，陪数，赔率）
        had_data = [float(had.get("h")),float(had.get("d")),float(had.get("a"))]
    # 让球胜平负
    hhad = oddsHistory.get("hhadList")[-1]
    # 让球数
    goalLine = hhad.get("goalLine")
    hspf = {"hh": float(hhad.get("h")), "hd": float(hhad.get("d")), "ha": float(hhad.get("a"))}
    hspfv = [float(hhad.get("h")), float(hhad.get("d")), float(hhad.get("a"))]
    # 转化带有赔数的数据格式{"s0":[10.5,6]}
    res_hspf = to_pei(hspf)
    peid_hspf = to_pei_list(hspf)
    hspf_result = [hspfr,res_hspf.get(hspfr)[1],res_hspf.get(hspfr)[0]]      # 让球胜平负开奖结果（结果，陪数，赔率）
    hhad_data = [float(hhad.get("h")),float(hhad.get("d")),float(hhad.get("a"))]
    # 构造分析数据
    data = had_data + hhad_data
    # data 是拿去比对的数据，方便查找相同数据的比赛
    res = {"NO": m.get("matchNumStr"), "goalLine": goalLine,"awayTeamAbbName": awayTeamAbbName, "homeTeamAbbName": homeTeamAbbName,
             "details": {"had": res_spf, "hspf": res_hspf}, "matchResult":{"spf":spf_result,"hspf":hspf_result},"peid_spf": peid_spf, "peid_hspf": peid_hspf,
             "spfv": spfv, "hspfv": hspfv, "matchDate": m.get("matchDate"), "matchTime": m.get("matchTime"),
             "matchId": m.get("matchId"),"data": data}
    return res